package mypazar.salesman;

import mypazar.agent.PersonAgent;
import mypazar.jenamodel.PazarVocabulary;
import tr.edu.ege.seagent.planner.htn.Action;
import tr.edu.ege.seagent.planner.htn.ExecutionMethod;
import tr.edu.ege.seagent.planner.htn.Outcome;
import tr.edu.ege.seagent.planner.htn.Provision;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

/**
 * Action class of ACReceiveProductQuery.
 */
public class ACReceiveProductQuery extends Action {

	/**
	 * Product provision.
	 */
	@Provision
	private String Product;

	/**
	 * Returns the value of the Product provision.
	 */
	public String getProduct() {
		return this.Product;
	}

	/**
	 * Sets the value of the Product provision.
	 */
	public void setProduct(String Product) {
		this.Product = Product;
	}

	/**
	 * ProductPrice outcome.
	 */
	@Outcome
	private Integer ProductPrice;

	/**
	 * Returns the value of the ProductPrice outcome.
	 */
	public Integer getProductPrice() {
		return this.ProductPrice;
	}

	/**
	 * Sets the value of the ProductPrice outcome.
	 */
	public void setProductPrice(Integer ProductPrice) {
		this.ProductPrice = ProductPrice;
	}

	private static final Property PRODUCT_PROP = ResourceFactory
			.createProperty(PazarVocabulary.PRODUCT_URI);

	/**
	 * Execution method.
	 */
	@ExecutionMethod
	public void execute() {
		Model beliefBase = (Model) getHeapValue(PersonAgent.BELIEF_BASE_ITEM_NAME);
		String SALESMAN_URI = PazarVocabulary.NS_URI
				+ getAgentIdentifier().toString().substring(0, 9);

		String productName = getProductNameFromBeliefBase(beliefBase,
				SALESMAN_URI);

		if (productName.equals(getProduct())) {
			this.setProductPrice(getPriceFromBeliefBase(beliefBase,
					SALESMAN_URI));
			this.setOutcomeState("Inform");
		} else
			this.setOutcomeState("Failure");
	}

	private String getProductNameFromBeliefBase(Model beliefBase,
			String salesmanURI) {
		String pName = null;

		Resource resSalesman = beliefBase.getResource(salesmanURI);
		StmtIterator iterProduct = resSalesman.listProperties(PRODUCT_PROP);
		if (iterProduct.hasNext()) {
			Statement stmtProduct = iterProduct.next();
			Resource resProduct = stmtProduct.getObject().asResource();

			pName = resProduct
					.getProperty(
							ResourceFactory
									.createProperty(PazarVocabulary.PRODUCT_NAME_URI))
					.getLiteral().getString();
		}

		return pName;
	}

	private Integer getPriceFromBeliefBase(Model beliefBase, String salesmanURI) {
		int pPrice = 0;
		Resource resSalesman = beliefBase.getResource(salesmanURI);
		StmtIterator iterProduct = resSalesman.listProperties(PRODUCT_PROP);
		if (iterProduct.hasNext()) {
			Statement stmtProduct = iterProduct.next();
			Resource resProduct = stmtProduct.getObject().asResource();

			pPrice = resProduct
					.getProperty(
							ResourceFactory
									.createProperty(PazarVocabulary.PRICE_URI))
					.getLiteral().getInt();
		}
		return pPrice;
	}
}